package view;

import control.Application;
import model.PAppGrid;
import model.PAppRect;
import model.PathOptions;
import processing.core.*;

@SuppressWarnings("serial")
public class CanvasPApplet extends PApplet {
	private PAppGrid grid;
	private int height;
	private int width;
	private int sideSquares;
	private PathOptions option=PathOptions.NORMAL;
	private int[][] colors = {	{255,255,255},		//Normal path color BLANCO
															{20,131,35},			//Grass path color VERDE	
															{205,201,201},		//Rocks path color GRIS
															{0,153,153},			//Player color AZUL
															{255,255,0},			//Finish line color AMARILLO
															{255,0,0}};				//Solucion al laberinto			
	
	public CanvasPApplet(int panelWidth, int panelHeight, int sideSquares) {
		height=panelHeight;
		width=panelWidth;		
		this.sideSquares=sideSquares;
		grid = new PAppGrid(sideSquares);
	}
	public void setup() {
	  size(width,height);
	  background(80);
	}
	public void draw() {
	  if(mousePressed&&mouseX<width&&mouseY<height&&mouseX>0&&mouseY>0){
  		PAppRect rect = grid.getSquareGrid()[sideSquares*mouseY/height][sideSquares*mouseX/width];
	  	if(mouseButton==LEFT){
	  		if(option.equals(PathOptions.PLAYER)){	  			
	  			int players = Application.getInstance().getNumberPlayers();	  			
	  			if(players>0){
	  				return;
	  			}
	  			Application.getInstance().setNumberPlayers(players+1);
	  		}
	  		else if(option.equals(PathOptions.EXIT)){	  			
	  			int exits = Application.getInstance().getNumberExits();	  			
	  			if(exits>0){
	  				return;
	  			}
	  			Application.getInstance().setNumberExits(exits+1);
	  		}
	  		else if(rect.getActiveOption().equals(PathOptions.EXIT)){
	  			Application.getInstance().setNumberExits(0);
	  		}
	  		else if(rect.getActiveOption().equals(PathOptions.PLAYER)){
	  			Application.getInstance().setNumberPlayers(0);
	  		}
	  		if(rect.getActiveOption().equals(PathOptions.WALL)){
	  			Application.getInstance().setPathNodesCounter(Application.getInstance().getPathNodesCounter()+1);
	  		}
	  		
	  		rect.setPath(true);
	  		rect.setActiveOption(option);
	  	}
	  	else if(mouseButton==RIGHT){
	  		rect.setPath(false);
	  		if(rect.getActiveOption().equals(PathOptions.PLAYER)){
	  			Application.getInstance().setNumberPlayers(0);
	  		}
	  		else if(rect.getActiveOption().equals(PathOptions.EXIT)){
	  			Application.getInstance().setNumberExits(0);
	  		}
	  		if(!rect.getActiveOption().equals(PathOptions.WALL)){
	  			Application.getInstance().setPathNodesCounter(Application.getInstance().getPathNodesCounter()-1);
	  		}
	  		rect.setActiveOption(PathOptions.WALL);
	  	}
	  }
	  for (PAppRect[] row:grid.getSquareGrid()) {
      //for each number in the row
      for (PAppRect rect : row) {
      	stroke(255);
  	  	fill(0);
  	  	if(rect.isPath()==true){
  	  	  stroke(0);
  	  	  int color[] = fillColor(rect); 
  	  	  fill(color[0],color[1],color[2]);
  	  	}
  	  	rect(rect.getPosX(), rect.getPosY(), rect.getSideLenghtX(), rect.getSideLenghtY());
      }
	  }
	}
	public void setActiveOption(PathOptions newOption){
		option=newOption;
	}
	private int[] fillColor(PAppRect rect){
		return colors[rect.getActiveOption().ordinal()];
	}
	public PAppGrid getGrid() {
		return grid;
	}
	public void setGrid(PAppGrid grid) {
		this.grid = grid;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public void resetGrid(){		
		for (PAppRect[] row:grid.getSquareGrid()) {
      //for each number in the row
      for (PAppRect rect : row) {
      	rect.setPath(false);
      	rect.setActiveOption(PathOptions.WALL);
      }
	  }
		Application.getInstance().setNumberExits(0);
		Application.getInstance().setNumberPlayers(0);
		Application.getInstance().setPathNodesCounter(0);
	}
}
